import configparser
import os
from datetime import datetime,timedelta
import time


def get_config(section, key=None):
    config = configparser.ConfigParser()
    file_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'config/app.ini')
    config.read(file_path)
    items = config.items(section)
    ret = {}
    for k, v in items:
        ret[k] = v
    if key:
        return ret.get(key)
    return ret


def get_token():
    return get_config('TU', 'token')


def fill_placeholder(field_len):
    return '(' + ('%s,' * field_len).rstrip(',') + '),'


def update_fields_sql(fields):
    sql = ' ON DUPLICATE KEY UPDATE '
    for field in fields:
        sql += "%s=values(%s)," % (field, field)
    return sql.rstrip(',')


def today():
    return datetime.now().strftime('%Y%m%d')


def diff_day(n):
    return (datetime.now() + timedelta(days=n)).strftime('%Y%m%d')


def diff_date(date, n):
    # 将其转换为时间数组
    timeArray = time.strptime(date, "%Y%m%d")
    # 转换为时间戳:
    dateArray = datetime.utcfromtimestamp(int(time.mktime(timeArray)) + n * 86400)
    return dateArray.strftime("%Y%m%d")


def weekday():
    return int(datetime.now().strftime('%w'))


def table_suffix(date):
    return '_' + date[:4]


def array_chunk(arr, n):
    total = len(arr)
